﻿Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols

<WebService(Namespace:="http://www.resamu.org/")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Public Class fixml
    Inherits System.Web.Services.WebService

    <WebMethod()> _
    Public Function Test(ByVal idresamu As String, ByVal pwd As String) As String
        Dim message As String
        Dim USERLOG As New USER.user
        USERLOG.Timeout = -1
        'penser à gérer l'adresse du WS
        USERLOG.Url = Application("WSSECURITE")
        message = USERLOG.Identification(idresamu, pwd)
        Dim LOG As New LOGSURVEY.logsurvey
        LOG.Timeout = -1
        LOG.Url = Application("WSLOG")
        LOG.NewLog(idresamu, "WSFIXML", "WSFIXML", "TEST", message)
        Return message
    End Function
    <WebMethod()> _
    Public Function GetFixmlStructure(ByVal idresamu As String, ByVal pwd As String) As System.Xml.XmlDocument
        Dim message As String
        Dim xmldata As New System.Xml.XmlDocument
        Dim USERLOG As New USER.user
        'penser à gérer l'adresse du WS
        USERLOG.Url = Application("WSSECURITE")
        USERLOG.Timeout = -1
        message = USERLOG.Identification(idresamu, pwd)
        Dim data As String() = message.Split("/")
        If data(0) = "IDOK" Then
            xmldata.Load(Server.MapPath("App_Data\fixml.xml"))
        End If
        Return xmldata
    End Function
    <WebMethod()> _
    Public Function NewFiXml(ByVal idresamu As String, ByVal pwd As String, ByVal idteam As String, _
    ByVal idpers As String, ByVal fixml As System.Xml.XmlDocument) As String

        Dim message, securite(), localid, resaid, commune, codep, pays, utmx, utmy As String
        Dim dbFixml As New ResaOPDataSetTableAdapters.itv_listTableAdapter

        Dim USERLOG As New USER.user
        USERLOG.Url = Application("WSSECURITE")
        USERLOG.Timeout = -1

        Dim ResaMessage As New ResaMessage.ResaMessage
        ResaMessage.Url = Application("ResaMessage")
        ResaMessage.Timeout = -1



        Try
            securite = USERLOG.Identification(idresamu, pwd).Split("/")
            Try
                If securite(0) = "IDOK" Then
                    'on analyse le fichier xml transmis
                    localid = fixml.DocumentElement.SelectSingleNode("id").SelectSingleNode("id").InnerText
                    commune = fixml.DocumentElement.SelectSingleNode("adresse").SelectSingleNode("com").InnerText
                    codep = fixml.DocumentElement.SelectSingleNode("adresse").SelectSingleNode("codep").InnerText
                    pays = fixml.DocumentElement.SelectSingleNode("adresse").SelectSingleNode("pays").InnerText
                    utmx = fixml.DocumentElement.SelectSingleNode("adresse").SelectSingleNode("utmx").InnerText
                    utmy = fixml.DocumentElement.SelectSingleNode("adresse").SelectSingleNode("utmy").InnerText
                    'on génére une idresamu pour l'itv
                    resaid = CStr(Now.Year) + CStr(Now.Month) + CStr(Now.Day) + CStr(Now.Hour) + _
                    CStr(Now.Minute) + CStr(Now.Second) + CStr(Now.Millisecond) + CStr(Now.Ticks)
                    'on insère l'identite dans le doc
                    Dim xmlnode As System.Xml.XmlNode
                    xmlnode = fixml.DocumentElement.SelectSingleNode("id").SelectSingleNode("resaid")
                    xmlnode.InnerText = resaid
                    'on insére l'itv dans la base
                    dbFixml.Insert(resaid, localid, Now, idresamu, idteam, securite(5), _
                    commune, codep, pays, utmx, utmy, Now, idresamu, idteam, fixml.InnerXml)
                    'WS de messagerie nouvelle itv
                    'Dim xmlnodeEquipe As System.Xml.XmlNode
                    'Dim xmlliste As System.Xml.XmlNodeList
                    ''on liste les noeud équipes dans le document
                    'xmlliste = fixml.DocumentElement.SelectNodes("equipe")
                    'For Each xmlnodeEquipe In xmlliste
                    '    Dim destid, destequipe As String
                    '    destid = xmlnodeEquipe.SelectSingleNode("idresamu").InnerText
                    '    destequipe = xmlnodeEquipe.SelectSingleNode("idteam").InnerText
                    '    ResaMessage.NewMessageSystem(destid, destequipe, resaid, "NEWITV")
                    'Next
                    message = "NEWFIXMLOK/" + resaid
                Else
                    message = "ERROR/INTRUSION"
                End If
            Catch ex As Exception
                message = "ERROR/" + ex.Message
            End Try
        Catch ex As Exception
            message = "ERROR/" + ex.Message
        End Try
        Return message
    End Function
    <WebMethod()> _
    Public Function GetFixml(ByVal idresamu As String, ByVal pwd As String, _
    ByVal idteam As String, ByVal idpers As String, ByVal resaid As String) As System.Xml.XmlDocument
        Dim USERLOG As New USER.user
        'penser à gérer l'adresse du WS
        USERLOG.Url = Application("WSSECURITE")
        USERLOG.Timeout = -1
        Dim securite() As String
        Dim xmltrans As New System.Xml.XmlDocument
        Dim xmlnode As System.Xml.XmlNode
        Dim dbFIXML As New ResaOPDataSetTableAdapters.itv_listTableAdapter
        Dim data As New ResaOPDataSet.itv_listDataTable
        Try
            securite = USERLOG.Identification(idresamu, pwd).Split("/")
            If securite(0) = "IDOK" Then
                data = dbFIXML.GetDataByResaid(resaid)
                xmltrans.LoadXml(data(0).fixml)
            Else
                xmltrans.Load(Server.MapPath("App_Data\trans.xml"))
                xmlnode = xmltrans.DocumentElement.SelectSingleNode("message")
                xmlnode.InnerText = "ERROR/INTRUSION"
            End If
        Catch ex As Exception
            xmltrans.Load(Server.MapPath("App_Data\trans.xml"))
            xmlnode = xmltrans.DocumentElement.SelectSingleNode("message")
            xmlnode.InnerText = "ERROR/" + ex.Message
        End Try
        'todo brancher ici WS de log
        Return xmltrans
    End Function
    <WebMethod()> _
    Public Function UpdateFixml(ByVal idresamu As String, ByVal pwd As String, ByVal idteam As String, _
    ByVal idpers As String, ByVal resaid As String, ByVal fixml As System.Xml.XmlDocument) As String
        Dim USERLOG As New USER.user
        USERLOG.Url = Application("WSSECURITE")
        USERLOG.Timeout = -1
        Dim message, securite(), localid, commune, codep, pays, utmx, utmy As String
        Dim old_fixml As New System.Xml.XmlDocument
        Dim old_node, node As System.Xml.XmlNode
        Dim old_list, list As System.Xml.XmlNodeList
        Dim dbFixml As New ResaOPDataSetTableAdapters.itv_listTableAdapter
        Dim data As New ResaOPDataSet.itv_listDataTable
        Dim flag_node As Boolean
        Dim element1, element2 As System.Xml.XmlElement
        Try
            securite = USERLOG.Identification(idresamu, pwd).Split("/")
            Try
                If securite(0) = "IDOK" Then
                    'on analyse le fichier xml transmis
                    localid = fixml.DocumentElement.SelectSingleNode("id").SelectSingleNode("id").InnerText
                    commune = fixml.DocumentElement.SelectSingleNode("adresse").SelectSingleNode("com").InnerText
                    codep = fixml.DocumentElement.SelectSingleNode("adresse").SelectSingleNode("codep").InnerText
                    pays = fixml.DocumentElement.SelectSingleNode("adresse").SelectSingleNode("pays").InnerText
                    utmx = fixml.DocumentElement.SelectSingleNode("adresse").SelectSingleNode("utmx").InnerText
                    utmy = fixml.DocumentElement.SelectSingleNode("adresse").SelectSingleNode("utmy").InnerText
                    'on analyse les différents noeuds obs
                    'Etape 1 on récupère le fichier fixml correspondant
                    data = dbFixml.GetDataByResaid(resaid)
                    old_fixml.LoadXml(data(0).fixml)
                    'on liste les noeuds obs ds old_fixml
                    old_list = old_fixml.DocumentElement.SelectNodes("obs")
                    list = fixml.DocumentElement.SelectNodes("obs")
                    For Each old_node In old_list
                        flag_node = False
                        For Each node In list
                            If node.SelectSingleNode("id").InnerText = old_node.SelectSingleNode("id").InnerText Then
                                flag_node = True
                                Exit For
                            End If
                        Next
                        Select Case flag_node
                            Case True
                            Case False
                                element1 = fixml.CreateElement("obs")

                                element2 = fixml.CreateElement("id")
                                element2.InnerText = old_node.SelectSingleNode("id").InnerText
                                element1.AppendChild(element2)

                                element2 = fixml.CreateElement("texte")
                                element2.InnerText = old_node.SelectSingleNode("texte").InnerText
                                element1.AppendChild(element2)

                                fixml.DocumentElement.AppendChild(element1)
                        End Select
                    Next
                    'on maj l'itv dans la base
                    dbFixml.UpdateByIdResamu(commune, codep, pays, utmx, utmy, Now, idresamu, _
                    idteam, fixml.InnerXml, resaid)
                    message = "FIXMLUPDATEOK/" + resaid
                Else
                    message = "ERROR/INTRUSION"
                End If
            Catch ex As Exception
                message = "ERROR/" + ex.Message
            End Try
        Catch ex As Exception
            message = "ERROR/" + ex.Message
        End Try
        'todo brancher ici WS de messagerie nouvelle itv
        Return message
    End Function
    <WebMethod()> _
    Public Function NewFiXmlString(ByVal idresamu As String, ByVal pwd As String, ByVal idteam As String, _
    ByVal idpers As String, ByVal fixml_s As String) As String
        Dim USERLOG As New USER.user
        USERLOG.Url = Application("WSSECURITE")
        USERLOG.Timeout = -1
        Dim fixml As New System.Xml.XmlDocument
        fixml.LoadXml(fixml_s)
        Dim message, securite(), localid, resaid, commune, codep, pays, utmx, utmy As String
        Dim dbFixml As New ResaOPDataSetTableAdapters.itv_listTableAdapter
        Try
            securite = USERLOG.Identification(idresamu, pwd).Split("/")
            Try
                If securite(0) = "IDOK" Then
                    'on analyse le fichier xml transmis
                    localid = fixml.DocumentElement.SelectSingleNode("id").SelectSingleNode("id").InnerText
                    commune = fixml.DocumentElement.SelectSingleNode("adresse").SelectSingleNode("com").InnerText
                    codep = fixml.DocumentElement.SelectSingleNode("adresse").SelectSingleNode("codep").InnerText
                    pays = fixml.DocumentElement.SelectSingleNode("adresse").SelectSingleNode("pays").InnerText
                    utmx = fixml.DocumentElement.SelectSingleNode("adresse").SelectSingleNode("utmx").InnerText
                    utmy = fixml.DocumentElement.SelectSingleNode("adresse").SelectSingleNode("utmy").InnerText
                    'on génére une idresamu pour l'itv
                    resaid = CStr(Now.Year) + CStr(Now.Month) + CStr(Now.Day) + CStr(Now.Hour) + _
                    CStr(Now.Minute) + CStr(Now.Second) + CStr(Now.Millisecond) + CStr(Now.Ticks)
                    'on insère l'identite dans le doc
                    Dim xmlnode As System.Xml.XmlNode
                    xmlnode = fixml.DocumentElement.SelectSingleNode("id").SelectSingleNode("resaid")
                    xmlnode.InnerText = resaid
                    'on insére l'itv dans la base
                    dbFixml.Insert(resaid, localid, Now, idresamu, idteam, securite(5), _
                    commune, codep, pays, utmx, utmy, Now, idresamu, idteam, fixml.InnerXml)
                    message = "NEWFIXMLOK/" + resaid
                    'todo brancher ici WS de messagerie nouvelle itv
                Else
                    message = "ERROR/INTRUSION"
                End If
            Catch ex As Exception
                message = "ERROR/" + ex.Message
            End Try
        Catch ex As Exception
            message = "ERROR/" + ex.Message
        End Try
        Return message
    End Function
    <WebMethod()> _
    Public Function UpdateFiXmlString(ByVal idresamu As String, ByVal pwd As String, ByVal idteam As String, _
    ByVal idpers As String, ByVal resaid As String, ByVal fixml_s As String) As String
        Dim USERLOG As New USER.user
        USERLOG.Url = Application("WSSECURITE")
        USERLOG.Timeout = -1
        Dim message, securite(), localid, commune, codep, pays, utmx, utmy As String
        Dim old_fixml As New System.Xml.XmlDocument
        Dim old_node, node As System.Xml.XmlNode
        Dim old_list, list As System.Xml.XmlNodeList
        Dim dbFixml As New ResaOPDataSetTableAdapters.itv_listTableAdapter
        Dim data As New ResaOPDataSet.itv_listDataTable
        Dim flag_node As Boolean
        Dim fixml As New System.Xml.XmlDocument
        fixml.LoadXml(fixml_s)
        Dim element1, element2 As System.Xml.XmlElement
        Try
            securite = USERLOG.Identification(idresamu, pwd).Split("/")
            Try
                If securite(0) = "IDOK" Then
                    'on analyse le fichier xml transmis
                    localid = fixml.DocumentElement.SelectSingleNode("id").SelectSingleNode("id").InnerText
                    commune = fixml.DocumentElement.SelectSingleNode("adresse").SelectSingleNode("com").InnerText
                    codep = fixml.DocumentElement.SelectSingleNode("adresse").SelectSingleNode("codep").InnerText
                    pays = fixml.DocumentElement.SelectSingleNode("adresse").SelectSingleNode("pays").InnerText
                    utmx = fixml.DocumentElement.SelectSingleNode("adresse").SelectSingleNode("utmx").InnerText
                    utmy = fixml.DocumentElement.SelectSingleNode("adresse").SelectSingleNode("utmy").InnerText
                    'on analyse les différents noeuds obs
                    'Etape 1 on récupère le fichier fixml correspondant
                    data = dbFixml.GetDataByResaid(resaid)
                    old_fixml.LoadXml(data(0).fixml)
                    'on liste les noeuds obs ds old_fixml
                    old_list = old_fixml.DocumentElement.SelectNodes("obs")
                    list = fixml.DocumentElement.SelectNodes("obs")
                    For Each old_node In old_list
                        flag_node = False
                        For Each node In list
                            If node.SelectSingleNode("id").InnerText = old_node.SelectSingleNode("id").InnerText Then
                                flag_node = True
                                Exit For
                            End If
                        Next
                        Select Case flag_node
                            Case True
                            Case False
                                element1 = fixml.CreateElement("obs")

                                element2 = fixml.CreateElement("id")
                                element2.InnerText = old_node.SelectSingleNode("id").InnerText
                                element1.AppendChild(element2)

                                element2 = fixml.CreateElement("texte")
                                element2.InnerText = old_node.SelectSingleNode("texte").InnerText
                                element1.AppendChild(element2)

                                fixml.DocumentElement.AppendChild(element1)
                        End Select
                    Next
                    'on maj l'itv dans la base
                    dbFixml.UpdateByIdResamu(commune, codep, pays, utmx, utmy, Now, idresamu, _
                    idteam, fixml.InnerXml, resaid)
                    message = "FIXMLUPDATEOK/" + resaid
                Else
                    message = "ERROR/INTRUSION"
                End If
            Catch ex As Exception
                message = "ERROR/" + ex.Message
            End Try
        Catch ex As Exception
            message = "ERROR/" + ex.Message
        End Try
        'todo brancher ici WS de messagerie nouvelle itv
        Return message
    End Function
    <WebMethod()> _
    Public Function GetModifId(ByVal idresamu As String, ByVal pwd As String, ByVal idteam As String, _
    ByVal idpers As String, ByVal resaid As String) As String
        Dim USERLOG As New USER.user
        USERLOG.Url = Application("WSSECURITE")
        USERLOG.Timeout = -1
        Dim message, securite() As String
        Dim dbFIXML As New ResaOPDataSetTableAdapters.itv_listTableAdapter
        Dim data As New ResaOPDataSet.itv_listDataTable
        Try
            securite = USERLOG.Identification(idresamu, pwd).Split("/")
            If securite(0) = "IDOK" Then
                data = dbFIXML.GetDataByResaid(resaid)
                message = data(0).modif_id + "/" + data(0).modif_team
            Else
                message = "ERROR/INTRUSION"
            End If
        Catch ex As Exception
            message = "ERROR/" + ex.Message
        End Try
        Return message
    End Function

End Class
